/**
 *
 */
package br.com.werp.regras.rh.integracao;

import br.com.werp.regras.geral.Debuger;
import br.com.werp.regras.geral.ErroPadrao;
import br.com.werp.regras.geral.integracao.MasterDAO;
import br.com.werp.regras.geral.util.Constantes;
import br.com.werp.regras.geral.util.Dia;
import br.com.werp.regras.geral.util.FormatDate;
import br.com.werp.regras.geral.util.Util;
import br.com.werp.regras.geral.vos.tabelas.Noticia;
import br.com.werp.regras.geral.vos.tabelas.Usuario_empresa;
import br.com.werp.regras.rh.controle.ControleNoticia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.List;
import java.util.ResourceBundle;


/**
 * @author Italo
 *
 *07/07/2006
 */
public class NoticiaDAO extends MasterDAO {
    private static NoticiaDAO noticiaDAO;
    private static String RESGATAR_ID = "RESGATAR_ID";
    private static String EXCLUIR_NOTICIA = "EXCLUIR_NOTICIA";

    private NoticiaDAO(String banco) throws Exception {
        super(banco);
    }

    public static NoticiaDAO getNoticiaDAO() throws Exception {
        if (noticiaDAO == null) {
            ResourceBundle bundle = ResourceBundle.getBundle(Constantes.CONFIG);
            noticiaDAO = new NoticiaDAO(bundle.getString(Constantes.BANCO));
        }

        return noticiaDAO;
    }

    public void inserirNoticia(Noticia noticia, Object key)
        throws Exception {
        Connection connection = validarTransacao(key);

        inserirVO(connection, noticia, new String[] { "noti_id" });
    }

    public void atualizarNoticia(Noticia noticia, Object key)
        throws Exception {
        Connection connection = validarTransacao(key);
        atualizarVO(connection, noticia, new String[] { "noti_id" });
    }

    public Usuario_empresa obterUsuarioEmpresa(String usuario)
        throws Exception {
        Connection connection = getConexao();

        try {
            Usuario_empresa usuario_empresa = new Usuario_empresa();
            usuario_empresa.setUsem_usuario(usuario);

            return (Usuario_empresa) Util.get0(pesquisarVO(connection,
                    usuario_empresa));
        } finally {
            liberaConeccao(connection);
        }
    }

    public List pesquisarNoticia(Noticia noticia) throws Exception {
        Connection connection = getConexao();

        try {
            String where = "1=1";

            if ((noticia.getNoti_id() != 0)) {
                where += (" and noti_id = " + noticia.getNoti_id());
            }

            if (noticia.getNoti_data() != null) {
                where += (" and noti_data like " +
                aspasLike(FormatDate.format(noticia.getNoti_data(), "yyyy-MM-dd")));
            }

            return preencheVO(connection, new Noticia(), where);
        } finally {
            liberaConeccao(connection);
        }
    }

    public String pesquisarNoticiaID() throws Exception {
        Connection connection = getConexao();

        try {
            String retorno = "";

            String query = getQuery(RESGATAR_ID);

            PreparedStatement statement = connection.prepareStatement(query);

            ResultSet rs = statement.executeQuery(query);

            if (rs.first()) {
                retorno = rs.getString("noti_id");
            }

            return retorno;
        } finally {
            liberaConeccao(connection);
        }
    }

    public static void main(String[] args) throws Exception {
        noticiaDAO = getNoticiaDAO();

        String id = noticiaDAO.pesquisarNoticiaID();

        System.out.println(id);
    }

    public void excluirNoticia(Object key, Noticia noticia)
        throws Exception {
        Connection connection = getConexao();

        String query = getQuery(EXCLUIR_NOTICIA);
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        setInt(1, preparedStatement, noticia.getNoti_id());
        preparedStatement.executeUpdate();
        Debuger.debugar("Excluindo noti_id = " + noticia.getNoti_descricao(),
            getClass());
    }
}
